package Test.Test02;

import java.util.Scanner;

public class MaxViewingDuration {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int rechargeAmount = scanner.nextInt();
        double packagePrice = scanner.nextDouble();
        int N = scanner.nextInt();

        // 读取优惠政策
        Policy[] policies = new Policy[N];
        for (int i = 0; i < N; i++) {
            int v1 = scanner.nextInt();
            int v2 = scanner.nextInt();
            int v3 = scanner.nextInt();
            policies[i] = new Policy(v1, v2, v3);
        }

        // 计算用户可以观看的最长时长
        int maxDuration = calculateMaxDuration(rechargeAmount, packagePrice, policies);
        System.out.println(maxDuration);

        scanner.close();
    }

    private static int calculateMaxDuration(int rechargeAmount, double packagePrice, Policy[] policies) {
        int maxDuration = 0;
        int remainingAmount = rechargeAmount;
        boolean canMultipleBuy = true;

        for (Policy policy : policies) {
            if (remainingAmount < policy.minMonth * packagePrice) {
                break;
            }

            if (policy.isMultipleBuy) {
                // 多买多送，计算可以购买的次数
                int buyTimes = (int) (remainingAmount / (policy.minMonth * packagePrice));
                maxDuration += policy.bonusMonth * buyTimes;
                remainingAmount -= buyTimes * policy.minMonth * packagePrice;
            } else {
                // 只享受一次，直接加上赠送时长
                maxDuration += policy.bonusMonth;
                break;
            }
        }

        // 加上剩余金额可以购买的时长
        maxDuration += (int) Math.floor(remainingAmount / packagePrice);

        return maxDuration;
    }

    static class Policy {
        int minMonth; // 最小购买时长
        boolean isMultipleBuy; // 是否多买多送
        int bonusMonth; // 赠送时长

        public Policy(int minMonth, int isMultipleBuy, int bonusMonth) {
            this.minMonth = minMonth;
            this.isMultipleBuy = isMultipleBuy == 1;
            this.bonusMonth = bonusMonth;
        }
    }
}
